package com.tianhui.consignortwofei.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.fgs.common.utils.LogUtil;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.PercentFormatter;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.interf.IChartViewInfo;
import com.tianhui.consignortwofei.utils.ApplicationUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author fgs
 * @date 2021/2/23
 * @description
 **/
public class PieChartView extends LinearLayout {
    private TextView titleTextView;
    private PieChart pieChart;

    public PieChartView(Context context) {
        this(context, null);
    }

    public PieChartView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PieChartView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.PieChartView);
        String title;
        try {
            title = typedArray.getString(R.styleable.PieChartView_PieChartViewTitle);
        } finally {
            typedArray.recycle();
        }
        View view = LayoutInflater.from(context).inflate(R.layout.layout_pie_chart_view, null);
        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(layoutParams);
        addView(view);

        titleTextView = view.findViewById(R.id.layout_pie_chart_view_titleTextView);
        pieChart = view.findViewById(R.id.layout_pie_chart_view_pieChartView);
        setPieCartTitle(title);
        initPieView();
    }

    public void setPieCartTitle(String title) {
        titleTextView.setText(title);
    }

    private void initPieView() {
        pieChart.setUsePercentValues(true);
        pieChart.setCenterText("");

        pieChart.animateX(2000);
        pieChart.setDrawCenterText(false);
        pieChart.setDrawEntryLabels(false);
        pieChart.setDrawHoleEnabled(false);
//        //显示圆环
//        pieChart.setDrawHoleEnabled(true);
//        pieChart.setHoleColor(Color.WHITE);
//        pieChart.setHoleRadius(58f);
        //圆环是否可旋转
        pieChart.setRotationEnabled(false);


        pieChart.getLegend().setEnabled(true);
        pieChart.setDescription(null);

    }

    public void setPieChartData(List<IChartViewInfo> list) {
        if (list == null) return;
        int size = list.size();
        IChartViewInfo info;

        if (size > 0) {
            ArrayList<PieEntry> entries = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                info = list.get(i);
                LogUtil.print(Float.valueOf(info.getValue())+"");
                entries.add(new PieEntry(Float.valueOf(info.getValue()), info.getLabel()));
            }

            PieDataSet dataSet = new PieDataSet(entries, "");
            dataSet.setValueFormatter(new PercentFormatter(pieChart) );
            //圆环颜色
            ArrayList<Integer> colors = new ArrayList<>();
            colors.add(ApplicationUtil.getContext().getResources().getColor(R.color.colorBlueLight));
            colors.add(ApplicationUtil.getContext().getResources().getColor(R.color.colorTwo));
            dataSet.setColors(colors);


            PieData pieData = new PieData(dataSet);
            pieData.setValueTextSize(11f);
            pieData.setValueTextColor(Color.WHITE);

            pieChart.setData(pieData);

//            for (IDataSet<?> set : pieChart.getData().getDataSets()) {
//
//                set.setDrawValues(true);
//            }

            pieChart.invalidate();
        }


    }
}
